package com.example.answer_staff;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.answer_staff.api.ApiManager;
import com.gaosai.staff.domain.MessageResult;
import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.utils.SPUtil;
import com.gaosai.staff.utils.UseCommInterface;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class ActivaActivity extends Activity {
	private StaffApplication application ;
	private EditText code;
	private EditText username;
	private EditText password;
	private EditText repassword;
	private Button activitybt;
	private String code1;
	private String password1;
	private String repassword1;
	private String uniqueId;
	private ImageView backjihuo;
	private ImageView question;
	private CheckBox cb;
	private ArrayList<String> user_info_list;
	private String staffid;
	private String userName;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//去掉标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
		//隐藏editText弹出的软键盘
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		setContentView(R.layout.activa);
		setViews();
		uniqueId=CommonUtils.getMyUUID(application);
		setListeners();
	}
	private void setListeners() {
		activitybt.setOnClickListener(new OnClickListener() {
			private boolean result_bolean;
			@Override
			public void onClick(View v) {
				code1=code.getText().toString();
				password1=password.getText().toString();
				repassword1=repassword.getText().toString();
				System.out.println("112"+password1.equals(repassword1));
				result_bolean = CommonUtils.accordance(password1,repassword1,application);
				if(result_bolean){
					/**激活*/
					ApiManager.getInstance().active(ActivaActivity.this, code1, null, password1, uniqueId, new AsyncHttpResponseHandler() {
						@Override
						public void onFailure(int arg0, Header[] arg1, byte[] arg2,
								Throwable arg3) {
						}
						@Override
						public void onFinish() {
							super.onFinish();
						}

						@Override
						public void onStart() {
							super.onStart();
						}
						@Override
						public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
							String result = new String(arg2) ;
							MessageResult messageResult = MessageResult.prase(result);
							if(0==messageResult.getCode()){
								SPUtil.putValue(getApplicationContext(), SPUtil.PASSWORD, password1);
								Toast.makeText(ActivaActivity.this, messageResult.getMsg(), 0).show() ;
								startActivity(new Intent(ActivaActivity.this,ActivaSucessActivity.class));
							}else{
								Toast.makeText(ActivaActivity.this, messageResult.getMsg(), 0).show() ;
							}
						}
					}) ;
				}
			}
		});
		backjihuo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(ActivaActivity.this,LoginActivity.class));
			}
		});


		question.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				UseCommInterface.getInstance().startActivity( ActivaActivity.this,PrivatePolicyActivity.class, null) ;
			}
		});

		cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
				activitybt.setBackgroundColor(Color.GREEN);
			}
		});
	}
	private void setViews() {
		user_info_list=getIntent().getStringArrayListExtra("user_info_list");
		for(int i=0;i<user_info_list.size();i++){
			String user_info_list_item = user_info_list.get(i);
			JsonParse(user_info_list_item);	
		}
		application = (StaffApplication) getApplication() ;
		code = (EditText)findViewById(R.id.editText1);
		username=(EditText)findViewById(R.id.editText2);
		code.setText(staffid);
		username.setText(userName);
		password=(EditText)findViewById(R.id.editText3);
		repassword=(EditText)findViewById(R.id.editText4);
		activitybt=(Button)findViewById(R.id.activitybt);
		backjihuo=(ImageView)findViewById(R.id.backjihuo);
		question=(ImageView)findViewById(R.id.question);
		cb=(CheckBox)findViewById(R.id.cb);
	}
	private void JsonParse(String  json) {
		JSONObject jsonObject=null;
		try {
			jsonObject = new JSONObject( json);
			staffid = jsonObject.getString("staffId");
			userName = jsonObject.getString("userName");
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}


}
